﻿@model HelloWorld.Code.DataAccess.User
@{
    ViewBag.Title = "PartialView Example";
}
<h2>@ViewBag.Title</h2>
<p>You are logged in as <strong>@Model.FirstName @Model.LastName</strong>.</p>
<hr/>
<p>Billing address:</p>
<div id="BillingAddress">
@Html.Partial("~/Views/PartialViews/AddressPartialView.cshtml", new ViewDataDictionary(Model.BillingAddress))
</div>
<hr/>
<p>Shipping address:</p>
<div id="ShippingAddress">
@Html.Partial("~/Views/PartialViews/AddressPartialView.cshtml", new ViewDataDictionary(Model.ShippingAddress))
</div>

@section scripts
{
    <script type="text/javascript">
        $().ready(function () {
            Init();
        });
 
        
    function Init() {
        $(".edit-address").click(function(event) {
            event.preventDefault();
            var link = $(this);
            var container = link.parent();
            $.ajax({
                url: '@Url.Action("EditLoggedUserAddress", "UserControls")',
                data: { type: container.attr('id') },
                type: 'GET',
                timeout: 300000, //5 minutes
                dataType: 'html',
                success: function(response) {
                    container.html(response);
                    Init();
                },
                error: function() {
                    alert('Error');
                }
            });
        });

        $(".save-address").click(function (event) {
            event.preventDefault();
            var link = $(this);
            var container = link.parent();
            $.ajax({
                url: '@Url.Action("SaveLoggedUserAddress", "UserControls")',
                data: container.children("input, textarea, select").serialize() + "&type=" + container.attr('id'),
                type: 'POST',
                timeout: 300000, //5 minutes
                dataType: 'html',
                success: function (response) {
                    container.html(response);
                    Init();
                },
                error: function () {
                    alert('Error');
                }
            });
        });
    }

;
    </script>
}